<?php

namespace App\Models\Traits;

use Illuminate\Support\Facades\Redis;

trait HasUserOrderRunningCache
{
	public function runningOrdersCacheKey(): string
	{
		return "user_{$this->id}_running_orders";
	}

	public function addOrderToRunningCache($cacheId)
	{
		Redis::zAdd($this->runningOrdersCacheKey(), 1, $cacheId);
	}

	public function delOrderFromRunningCache($cacheId)
	{
		Redis::zRem($this->runningOrdersCacheKey(), $cacheId);
	}

	public function isOrderInRunningCache($cacheId): bool
	{
		return Redis::zScore($this->runningOrdersCacheKey(), $cacheId) == 1;
	}
}
